package com.example.qjia.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qjia.Presenters.StudentMessagePresenter;
import com.example.qjia.Presenters.StudentMessagePresenterImp;
import com.example.qjia.R;
import com.example.qjia.basic.BaseFragment;
import com.example.qjia.data.StudentMessage;
import com.example.qjia.data.romotedata.Student;
import com.example.qjia.ui.ViewInterface.UserMesageInterface;
import com.example.qjia.ui.activity.LoginActivity;
import com.example.qjia.ui.activity.ModifyMessageActivity;


public class UserMessageFragment extends BaseFragment implements View.OnClickListener,UserMesageInterface {
    private TextView studentName;
    private TextView schoolSub;
    private TextView studentClass;
    private TextView studentNumber;
    private TextView studentPhoneNumber;
    private Button phoneNumberModifyBt;
    private TextView mailNumber;
    private Button phoneModifyBt;
    private Button mailModifyBt;
    private Button modifyPwdBt;
    private Button loginOutBt;
    private StudentMessagePresenter studentMessagePresenter;
    private static final String TAG="UserMessageFragment";
    public UserMessageFragment() { }
    public static UserMessageFragment newInstance() {
        UserMessageFragment fragment = new UserMessageFragment();
        return fragment;
    }
    @Override
    public void init(View view) {
        Log.i(TAG, "init: " );
        studentName=(TextView)view.findViewById(R.id.student_name);
        schoolSub=(TextView)view.findViewById(R.id.school_sub);
        studentClass=(TextView)view.findViewById(R.id.student_class);
        studentNumber=(TextView)view.findViewById(R.id.student_number);
        studentPhoneNumber=(TextView)view.findViewById(R.id.student_phonenumber);
        phoneNumberModifyBt=(Button)view.findViewById(R.id.modify_phonenumber);
        phoneNumberModifyBt.setOnClickListener(this);
        mailNumber=(TextView)view.findViewById(R.id.student_mail);

        mailModifyBt=(Button)view.findViewById(R.id.modify_mail);
        mailModifyBt.setOnClickListener(this);
        modifyPwdBt=(Button)view.findViewById(R.id.modify_pwd);
        modifyPwdBt.setOnClickListener(this);
        loginOutBt=(Button)view.findViewById(R.id.login_out);
        loginOutBt.setOnClickListener(this);
        studentMessagePresenter=new StudentMessagePresenterImp(this);
        studentMessagePresenter.loadStudentMessage();



    }

    @Override
    public int getResourceId() {
        return R.layout.fragment_user_message;
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.login_out)studentMessagePresenter.loginOut();
        if(view.getId()==R.id.modify_mail){             //修改邮箱
            Intent intent=new Intent(getContext(), ModifyMessageActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("modifyId",1);
            bundle.putString("modifyMessage",mailNumber.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);
        }
        if(view.getId()==R.id.modify_phonenumber){      //修改电话号码
            Intent intent=new Intent(getContext(), ModifyMessageActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("modifyId",2);
            bundle.putString("modifyMessage",studentPhoneNumber.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);
        }
        if(view.getId()==R.id.modify_pwd){           //修改密码
            Intent intent=new Intent(getContext(), ModifyMessageActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("modifyId",3);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    @Override
    public void setStudentMessage(Student studentMessage) {
        studentName.setText(studentMessage.getName());
        schoolSub.setText(studentMessage.getAcademy());;
        studentClass.setText(studentMessage.getMclass());
        studentNumber.setText(studentMessage.getStudentId());
        studentPhoneNumber.setText(studentMessage.getPhone());
        mailNumber.setText(studentMessage.getEmail());
    }
    @Override
    public void failed(){
        Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginOut() {
        Intent intent=new Intent(getActivity(), LoginActivity.class); //这里还有注销登陆信息
        startActivity(intent);

    }
}
